package com.qzcsbj.autotest.utils;

import com.qzcsbj.autotest.testcase.BaseCase;
import org.apache.log4j.Logger;
import org.testng.Assert;

import java.util.regex.Matcher;

/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
public class GlobalVariableUtil {
    public static Logger logger = Logger.getLogger(GlobalVariableUtil.class);

    /**
     * 从全局map中获取全局变量并替换
     * @param param
     * @return
     */
    public static String substitutionGlobalVariable(String param){
        if (!StringUtil.isNotNullAndEmpty(param)){
            return "";
        }
        Matcher matcher = BaseCase.replaceParamPattern.matcher(param);
        while (matcher.find()) {
            String replaceKey = matcher.group(1).trim();  // 得到第一个匹配内容
            String value;
            // 从全局变量map中获取值
            if ("".equals(replaceKey) || !BaseCase.globalVariableMap.containsKey(replaceKey)){
                value = null;
                Assert.fail("替换失败");  // 如果未能找到对应的值，该用例失败
            } else {
                value = BaseCase.globalVariableMap.get(replaceKey);
                // logger.info("value: " + value);
            }
            param = param.replace(matcher.group(), value);
        }
        return param;
    }

    public static void saveGlobalVariable(String actual, String globalVariables) {
    }
}